//
//  JFTabbarControllerConfig.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/5/3.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//
#import "SHBaseNavigationController.h"
#import "JFHomeViewController.h"
#import "JFSellerViewController.h"
#import "JFStoreViewController.h"
#import "JFMeViewController.h"
#import "JFTabbarControllerConfig.h"
@interface JFTabbarControllerConfig()
@property (nonatomic, readwrite, strong) CYLTabBarController *tabBarController;

@end
@implementation JFTabbarControllerConfig
/**
 *  lazy load tabBarController
 *
 *  @return CYLTabBarController
 */
- (CYLTabBarController *)tabBarController {
    if (_tabBarController == nil) {
        CYLTabBarController *tabBarController = [CYLTabBarController tabBarControllerWithViewControllers:self.viewControllers
            tabBarItemsAttributes:self.tabBarItemsAttributesForController];
               [self customizeTabBarAppearance:tabBarController];
    
        _tabBarController = tabBarController;
        
        
    }
    return _tabBarController;
}

- (NSArray *)viewControllers {
    JFHomeViewController *firstViewController = [[JFHomeViewController alloc] init];
    SHBaseNavigationController *firstNavigationController = [[SHBaseNavigationController alloc] initWithRootViewController:firstViewController];
    
    JFSellerViewController *secondViewController = [[JFSellerViewController alloc] init];
    SHBaseNavigationController *secondNavigationController = [[SHBaseNavigationController alloc] initWithRootViewController:secondViewController];
    
    JFStoreViewController *myinviteViewController = [[JFStoreViewController alloc] init];
    SHBaseNavigationController *thirdNavigationController = [[SHBaseNavigationController alloc]
        initWithRootViewController:myinviteViewController];
    
    JFMeViewController *fourthViewController = [[JFMeViewController alloc] init];
    SHBaseNavigationController *fourthNavigationController = [[SHBaseNavigationController alloc]
        initWithRootViewController:fourthViewController];

       NSArray *viewControllers = @[
                                 firstNavigationController,
                                 secondNavigationController,
                                 thirdNavigationController,
                                 fourthNavigationController,
                                 ];
    
    
    
    return viewControllers;
}

- (NSArray *)tabBarItemsAttributesForController {
   
    NSDictionary *firstTabBarItemsAttributes = @{
                                                 CYLTabBarItemTitle : @"首页",
                                                 CYLTabBarItemImage :@"home_tab",
                                                 CYLTabBarItemSelectedImage : @"home_tab_select",
                                                 };
    NSDictionary *secondTabBarItemsAttributes = @{
                                                 CYLTabBarItemTitle : @"电商",
                                                  CYLTabBarItemImage : @"merchant_tab",
                                                  CYLTabBarItemSelectedImage : @"merchant_tab_select",
                                                  };
    NSDictionary *thirdTabBarItemsAttributes = @{
                                                 CYLTabBarItemTitle :@"分享",
                                                 CYLTabBarItemImage : @"share_tab",
                                                 CYLTabBarItemSelectedImage : @"share_tab_select",
                                                 };
    NSDictionary *fourthTabBarItemsAttributes = @{
                                                  CYLTabBarItemTitle :@"我的",
                                                  CYLTabBarItemImage : @"my_tab",
                                                  CYLTabBarItemSelectedImage : @"my_tab_select"
                                                  };
   
    NSArray *tabBarItemsAttributes = @[
                                       firstTabBarItemsAttributes,
                                       secondTabBarItemsAttributes,
                                       thirdTabBarItemsAttributes,
                                       fourthTabBarItemsAttributes
                                       
                                       ];
    
    
    return tabBarItemsAttributes;
}

/**
 *  更多TabBar自定义设置：比如：tabBarItem 的选中和不选中文字和背景图片属性、tabbar 背景图片属性等等
 */
- (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
    
    // 普通状态下的文字属性
    // 设置文字颜色
   [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName :[UIColor colorWithHexString:@"#C5C5C5"],NSFontAttributeName : KSystemFont(10)} forState:UIControlStateNormal];
    // 设置文字颜色
    //选中状态
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#C28C42"],NSFontAttributeName : KSystemFont(10)} forState:UIControlStateSelected];
    
    //设置导航条背景
    
    [[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];

    [[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName:KSystemFont(__I6Width(17)),NSForegroundColorAttributeName:KSHTEXTCOLOR}];
    [[UINavigationBar appearance] setTranslucent:NO];
    [[UINavigationBar appearance] setTintColor:KSHTEXTCOLOR];
    [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabBar_bg"]];
    
    [UITabBar appearance].translucent = NO;
    
    // 设置文字属性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitlePositionAdjustment:UIOffsetMake(0,-4)];
    
    
    
}

- (void)updateTabBarCustomizationWhenTabBarItemWidthDidUpdate {
    void (^deviceOrientationDidChangeBlock)(NSNotification *) = ^(NSNotification *notification) {
        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
        if ((orientation == UIDeviceOrientationLandscapeLeft) || (orientation == UIDeviceOrientationLandscapeRight)) {
            NSLog(@"Landscape Left or Right !");
        } else if (orientation == UIDeviceOrientationPortrait) {
            NSLog(@"Landscape portrait!");
        }
        [self customizeTabBarSelectionIndicatorImage];
    };
    [[NSNotificationCenter defaultCenter] addObserverForName:CYLTabBarItemWidthDidChangeNotification
                                                      object:nil
                                                       queue:[NSOperationQueue mainQueue]
                                                  usingBlock:deviceOrientationDidChangeBlock];
}

- (void)customizeTabBarSelectionIndicatorImage {
    ///Get initialized TabBar Height if exists, otherwise get Default TabBar Height.
    UITabBarController *tabBarController = [self cyl_tabBarController] ?: [[UITabBarController alloc] init];
    CGFloat tabBarHeight = tabBarController.tabBar.frame.size.height;
    CGSize selectionIndicatorImageSize = CGSizeMake(CYLTabBarItemWidth, tabBarHeight);
    //Get initialized TabBar if exists.
    UITabBar *tabBar = [self cyl_tabBarController].tabBar ?: [UITabBar appearance];
    [tabBar setSelectionIndicatorImage:
     [[self class] imageWithColor:[UIColor redColor]
                             size:selectionIndicatorImageSize]];
}

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width + 1, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
